package com.zyz.O_网络编程.TCP.Thread;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author: vick
 * @Date: 2025/4/15
 * @Description:
 * @version: 1.0
 */
public class Client {
    public static void main(String[] args) throws IOException {
        //1. 创建Socket对象，连接服务器
        Socket socket = new Socket("192.168.0.106", 8888);

        //2.获取输出流，发送数据个服务端
        OutputStream outputStream = socket.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        //BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));

        //3. 获取输入流， 接收服务端返回的数据
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入发送给服务端的信息：");
            String message = scanner.nextLine();
            if(message.equals("stop")) {
                socket.shutdownOutput();
                break;
            }

            //4. 发送数据
            printStream.println(message);
            /*bufferedWriter.write(message);
            bufferedWriter.newLine();
            bufferedWriter.flush();*/

            //接收数据
            String back = bufferedReader.readLine();
            System.out.println("从服务端返回的数据：" + back);
        }

        //5. 关闭socket
        scanner.close();
        socket.close();
    }
}
